(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['leaflet'], factory);
    } else if (typeof module !== 'undefined') {
        // Node/CommonJS
        module.exports = factory(require('leaflet'));
    } else {
        // Browser globals
        if (typeof window.L === 'undefined') {
            throw new Error('Leaflet must be loaded first');
        }
        factory(window.L);
    }
}(function (L) {
    L.Control.Fullbounds = L.Control.extend({
        options: {
            position: 'topleft',
            title: '全图',
        },

        onAdd: function (map) {
            var container = L.DomUtil.create('div', 'leaflet-control-fullbounds leaflet-bar leaflet-control');

            this.link = L.DomUtil.create('a', 'leaflet-control-fullbounds-button leaflet-bar-part', container);
            this.link.href = '#';
            this.link.title = this.options.title;

            this._map = map;
            this._fullbounds = map.getBounds();
            L.DomEvent.on(this.link, 'click', this._click, this);
            return container;
        },

        _click: function (e) {
            L.DomEvent.stopPropagation(e);
            L.DomEvent.preventDefault(e);
            if(this._map.getBounds().equals(this._fullbounds)){
                console.log("当前地图范围为全图");
            }
            else{
                this._map.fitBounds(this._fullbounds);
            }
        },
    });

    L.Map.mergeOptions({
        fullboundsControl: false
    });

    L.Map.addInitHook(function () {
        if (this.options.fullboundsControl) {
            this.fullboundsControl = new L.Control.fullbounds(this.options.fullboundsControl);
            this.addControl(this.fullboundsControl);
        }
    });

    L.control.fullbounds = function (options) {
        return new L.Control.Fullbounds(options);
    };
}));